<?php

/**
 * 网站默认控制器
 *
 * @author JiangJian <silverd@sohu.com>
 * $Id: Uploadfile.php 162 2012-11-09 04:04:30Z Jun.Lu.726@gmail.com $
 */
class Controller_Uploadfile extends Controller_Abstract
{

    /**
     * 存储类型
     * @var type
     */
    private $fileType;

    public function init()
    {
        // 由于FLASH 不能使用COOKIES 所以传入SESSION ID 后启SESSION
        session_id($this->getx('token'));
        session_start();

        parent::init();

        $this->fileType = include (RESOURCE_PATH . 'upload.conf.php');
    }

    public function indexAction()
    {
        // 是否单张
        $numFlag = true;
        $tmpNum  = $this->getInt('num_flag');
        if ($tmpNum > 1) {
            $numFlag  = false;
        }
        // 类型 app img  或再添加
        $classify = $this->getx('classify');
        if (!isset($this->fileType[$classify])) {
            $this->jsonx('请正确上传类型！', 'err');
        }

        // 模块
        $module = $this->getx('module');
        if ('img' == $classify && empty($module) && !isset($this->fileType['img'][$module])) {
            $module = 'default';
        }

        // 初始
        $config = array(
            'savePath'     => dirname(APP_PATH) . DS . 'adproject' . DS . 'Web' . DS . 'uploads' . DS . $classify,
            'maxSize'      => 0,
            'maxWidth'     => 0,
            'maxHeight'    => 0,
            'allowedExts'  => '*',
            'allowedTypes' => '*',
            'override'     => false,
            'savePathHash' => true,
        );

        // 配置信息
        if ('app' == $classify) {
            $config = array_merge($config, $this->fileType['app']);
        }
        if ('img' == $classify) {
            $config = array_merge($config, $this->fileType['img'][$module]);
        }

        // 保存
        $obj = new Com_Upload($config);

        if ($a = $obj->save('Filedata')) {
            $log = Model('Uploads');
            if ($numFlag) {
                $tmpFIle = $obj->getSaveNames($numFlag);
                $logArr  = array(
                    'location' =>  2,
                    'classify'    => $classify,
                    'module'      => $module,
                    'suf'         => pathinfo($tmpFIle, PATHINFO_EXTENSION),
                    'file_path'   => '/' . $tmpFIle,//
                    'uid'         => $_SESSION['authUser']['admin_id'],
                    'create_time' => time(),
                );
                $savePath     = '/' . $tmpFIle;
                $log->insert($logArr);
            } else {
                foreach ($obj->getSaveNames($numFlag) as $val) {
                    $logArr[] = array(
                        'location' =>  2,
                        'classify'    => $classify,
                        'module'      => $module,
                        'suf'         => pathinfo($val, PATHINFO_EXTENSION),
                        'file_path'   => '/' . $val,
                        'uid'         => $_SESSION['authUser']['admin_id'],
                        'create_time' => time(),
                    );
                    $savePath[]   = '/' . $val;
                }
                $log->batchInsert($logArr);
            }
            $this->jsonx('上传成功！', 'success', array('filepath' => $savePath, 'rootpath' => '/uploads/' . $classify ));
        } else {
            // todo 上传错误日志

            $this->jsonx('上传失败！', 'err');
        }
    }

}